package com.yql.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.yql.entity.*;
import com.yql.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @RequestMapping("listOrder")
    public String listOrder(OrderVO orderVO, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize, Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        PageInfo<Order> info = orderService.listOrder(user,orderVO,pageNum,pageSize);
        model.addAttribute("info",info);
        model.addAttribute("vo",orderVO);
        model.addAttribute("statusValues", Status.values());
        return "listOrder";
    }

    @GetMapping("toListGoods")
    public String toListGoods(Long orderId,Model model){
        Order order = orderService.listOrderByOrderId(orderId);
        List<Goods> gList = orderService.listGoodsByOrderId(orderId);
        model.addAttribute("order",order);
        model.addAttribute("gList",gList);
        return "listGoods";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        Long orderId = snowflake.nextId();
        model.addAttribute("orderId",orderId);
        return "add_order";
    }

    @ResponseBody
    @RequestMapping("upFile")
    public String uploadFile(MultipartFile file) throws IOException {
        if(null!=file && !file.isEmpty()){
            String path = "g:/pic/";
            String originalFilename = file.getOriginalFilename();
            String s = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
            if(s.equals("jpg") || s.equals("git") || s.equals("png")){
                String filename = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                File f = new File(path, filename);
                file.transferTo(f);
                return "/pic/"+filename;
            }
        }
        return null;
    }

    @RequestMapping("add")
    public String addOrder(Order order,HttpSession session){
        User user = (User) session.getAttribute("user");
        order.setUser(user);
        orderService.addOrder(order);
        return "redirect:listOrder";
    }

    @ResponseBody
    @RequestMapping("deleteById")
    public boolean deleteById(Long id){
        Integer i = orderService.deleteById(id);
        if(i>0){
            Integer j = orderService.deleteMiddle(id);
            if(j>0){
                return true;
            }
        }
        return false;
    }
}
